﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Collections.Generic;
using System.Windows.Shapes;
using FishSimulation.Model;
using System.Windows.Input;

namespace FishSimulation.Views {
    /// <summary>
    /// Interaction logic for PondView.xaml
    /// </summary>
    public partial class PondView : ViewBase {
        #region Members
        Canvas canvas = null;
        #endregion

        #region Properties
        public Pond Pond {
            get;
            set;
        }
        #endregion

        #region Constructors
        public PondView() {
            InitializeComponent();
            this.Pond = Repository.Instance.World.Pond;
            this.Loaded += delegate {
                this.Bind();
            };
        }
        #endregion

        #region Event Handlers
        ActorInPondView dragged = null;

        public void OnMouseUp(MouseButtonEventArgs e) {            
            if (dragged != null) {                
                var pos = Mouse.GetPosition(this);
                dragged.Actor.X = pos.X;
                dragged.Actor.Y = pos.Y;
                dragged.Margin = new Thickness(pos.X, pos.Y, 10, 10);
                dragged = null;
            }
        }

        private void ViewBase_DragEnter(object sender, DragEventArgs e) {
            e.Effects = DragDropEffects.Move;
        }

        private void ViewBase_Drop(object sender, DragEventArgs e) {            
            ActorInPondView a = sender as ActorInPondView;
            if (a != null) {
                var pos = Mouse.GetPosition(this);
                a.Margin = new Thickness(pos.X, pos.Y, 10, 10);
            }
        }

        private void ViewBase_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
            ViewBase v = sender as ViewBase;
            if (v != null) {                
                //prevent whole screen from moving
                e.Handled = true;
                this.OnViewBaseClicked(v, e);
            }
            else {
                this.OnViewBaseClicked(this, e);
            }
        }
        
        private void ViewBase_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) {
            if (e.LeftButton == MouseButtonState.Pressed) {
                ActorInPondView a = sender as ActorInPondView;
                if (a != null) {
                    dragged = a;
                    DragDrop.DoDragDrop(a, a.Actor, DragDropEffects.Move);
                }
            }
        }
        #endregion

        #region Methods
        public void Bind() {            
            canvas = new Canvas();
            canvas.AllowDrop = true;
            canvas.Children.Clear();
            canvas.MouseDown += new System.Windows.Input.MouseButtonEventHandler(ViewBase_MouseDown);
            var path = new Path();
            //path.Stroke = System.Windows.Media.Brushes.Black;
            path.Fill = System.Windows.Media.Brushes.LightBlue;
            path.StrokeThickness = 4;
            path.HorizontalAlignment = HorizontalAlignment.Left;
            path.VerticalAlignment = VerticalAlignment.Center;
            
            var cords = new PathSegmentCollection(); 
            foreach(Cord c in this.Pond.Points) {
                cords.Add(new LineSegment() { Point = new Point(c.X, c.Y) });
            }

            PathFigureCollection f = new PathFigureCollection(
                new List<PathFigure>() { 
                    new PathFigure() {                            
                        Segments = cords
                    }
                }
            );
            
            path.Data = new PathGeometry() { Figures = f };
            canvas.Children.Add(path);

            children.Content = canvas;

            this.RenderActors();
        }

        public override ModelBase GetPropertyGridItem() {
            return this.Pond;
        }

        private void RenderActors() {
            foreach (Actor a in Repository.Instance.World.Actors) {
                ActorInPondView l = new ActorInPondView();
                l.Actor = a;
                l.Margin = new Thickness((double) a.X, (double) a.Y, 10, 10);                
                l.MouseDown += ViewBase_MouseDown;
                l.MouseMove += ViewBase_MouseMove;
                l.DragEnter += ViewBase_DragEnter;
                l.Drop += ViewBase_Drop;
                canvas.Children.Add(l);
            }
        }
        #endregion
    }
}
